//
//  LYMineViewController.m
//  artist
//
//  Created by Jay_Apple on 16/2/29.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYMineViewController.h"
#import "LYMineCellModel.h"
#import "LYMineHeaderView.h"
#import "LYMineHeaderModel.h"
#import "LYLoginInfoModel.h"
#import "LYMineHeaderItemModel.h"
#import "LYStudentDataViewController.h"

@interface LYMineViewController ()<UITableViewDataSource, UITableViewDelegate, LYMineHeaderViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *modelsArray;

@property (weak, nonatomic) LYMineHeaderView *header;

@end

@implementation LYMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.fd_prefersNavigationBarHidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self setUpThemeNotification];
    
}

- (void)dealloc{

    [self resignThemeNotification];
}

- (void)viewDidLayoutSubviews{

    [super viewDidLayoutSubviews];
    
    //初始化tableView
    [self setUpTableView];
    
    //主题元素
    [self upDateTheme];

}

#pragma mark - 重新

- (void)setModelsArray:(NSMutableArray *)modelsArray{

    _modelsArray = modelsArray;
    
    [self.tableView reloadData];

}

#pragma mark - 初始化
/**
 *  主题元素
 */
- (void)upDateTheme{

    //headerModel
    LYMineHeaderModel *model = [[LYDataSourceManager sharedManager] getMineHeaderModel];
    
    self.header.model = model;
    
    //minePList
   self.modelsArray = [LYMineCellModel mj_objectArrayWithFilename:[[LYDataSourceManager sharedManager] getMineListName]];
}


/**
 *  初始化tableview
 */
- (void)setUpTableView{
    
    //header
    LYMineHeaderView *header = [LYMineHeaderView viewForNib];
    CGFloat headerHeight = LYMineHeaderBGImageRatio * LYScreenWidth + LYMineHeaderBottomBarHeight;
    header.frame = CGRectMake(0, 0, LYScreenWidth, headerHeight);
    header.delegate = self;
    self.tableView.tableHeaderView = header;
    self.header = header;
    
    
}




#pragma mark - tableViewDataSource
/**
 *  cell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    LYMineCellModel *model = self.modelsArray[indexPath.section][indexPath.row];
    
    
    cell.imageView.image = [UIImage imageNamed:model.imageName];
    cell.textLabel.text = model.title;
    cell.textLabel.font = [UIFont systemFontOfSize:LYMineCellTextFont];
    cell.textLabel.textColor = [UIColor colorWithHexString:LYMineCellTextColor alpha:1.0];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.modelsArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.modelsArray[section] count];
}

#pragma mark - tableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return LYMineCellHeight;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYMineCellModel *model = self.modelsArray[indexPath.section][indexPath.row];
    
    if ([[VVYCheckStatus sharedInstance] checkIsLoginBlock:nil]){
        Class pushClass = NSClassFromString(model.classStr);
        UIViewController *vc = [[pushClass alloc] init];
        vc.navigationItem.title = model.title;
        [self.navigationController pushViewController:vc animated:YES];
    }

}


/**
 *  secitonHeader
 */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return LYMineSectionHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.1;
}

#pragma mark - LYMineHeaderViewDelegate
- (void)headerLoginBtnOnclick:(LYMineHeaderView *)header{

    [[VVYCheckStatus sharedInstance] checkIsLoginBlock:nil];
    
}

- (void)header:(LYMineHeaderView *)header bottomBtnOnclick:(LYMineHeaderItemModel *)itemModel{
    
    if ([[VVYCheckStatus sharedInstance] checkIsLoginBlock:nil]){
        Class pushClass = NSClassFromString(itemModel.className);
        UIViewController *vc = [[pushClass alloc] init];
        vc.navigationItem.title = itemModel.title;
        [self.navigationController pushViewController:vc animated:YES];
    }

}

- (void)headerIconOnclick:(LYMineHeaderView *)header{

    LYStudentDataViewController *personVC = [[LYStudentDataViewController alloc] initWithCanEditIdentity:NO];
    [self.navigationController pushViewController:personVC animated:YES];

}


@end
